package com.qms.repast.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;
import android.webkit.WebSettings;

import com.orhanobut.logger.Logger;
import com.qms.repast.commons.Constants;

import java.util.UUID;

/**
 * @author 泅渡者
 * @date 2018/8/31 0031
 * @describe: 系统助手类
 */
public class SystemUtils {
	/**
	 * 获取UA 用来解释当前使用的浏览器内核
	 *
	 * @param context
	 *
	 * @return
	 */
	public static String getUserAgent (Context context) {
		String userAgent = (String) SpUtils.getParam(context, Constants.USERAGENT, "");
		if (!TextUtils.isEmpty((userAgent))) {
			return userAgent;
		}
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
			try {
				userAgent = WebSettings.getDefaultUserAgent(context);
			} catch (Exception e) {
				userAgent = System.getProperty("http.agent");
			}
		} else {
			userAgent = System.getProperty("http.agent");
		}
		StringBuffer sb = new StringBuffer();
		for (int i = 0, length = userAgent.length(); i < length; i++) {
			char c = userAgent.charAt(i);
			if (c <= '\u001f' || c >= '\u007f') {
				sb.append(String.format("\\u%04x", (int) c));
			} else {
				sb.append(c);
			}
		}
		userAgent = sb.toString();
		SpUtils.setParam(context, Constants.USERAGENT, userAgent);
		return userAgent;
	}

	/**
	 * 获取版本号
	 *
	 * @return 当前应用的版本名称
	 */
	public static String getVersionName (Context context) {
		try {
			PackageManager manager = context.getPackageManager();
			PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
			String version = info.versionName + "";
			return "c_" + version;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "v1.0.0";
	}

	/**
	 * java 获取可变UUID
	 *
	 * @return
	 */
	public static String getMyUUID (Context context) {
		String uniqueId = SpUtils.getParam(context, Constants.UUID, "").toString();
		if (TextUtils.isEmpty(uniqueId)) {
			UUID uuid = UUID.randomUUID();
			uniqueId = uuid.toString().replaceAll("-", "");
			SpUtils.setParam(context, Constants.UUID, uniqueId);
		}
		return "android_" + uniqueId;
	}

	/**
	 * 获取用户session
	 *
	 * @param context
	 *
	 * @return
	 */
	public static String getSession (Context context) {
		return SpUtils.getParam(context, Constants.SESSION, "").toString();
	}

	/**
	 * @param context
	 *
	 * @return
	 */
	public static String getRegisterId (Context context) {
		return SpUtils.getParam(context, Constants.REGISTERID, "").toString();
	}

	/**
	 * 获取设备唯一标识符
	 *
	 * @return
	 */
	public static String getDeviceId () {
		String serial = null;
		String mszDevIDShort = "35" + Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +
				Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 +
				Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 + Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 +
				Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 + Build.TAGS.length() % 10 + Build.TYPE.length() % 10 +
				Build.USER.length() % 10; //13 位
		try {
			serial = android.os.Build.class.getField("SERIAL").get(null).toString();
			return new UUID(mszDevIDShort.hashCode(), serial.hashCode()).toString();

		} catch (Exception exception) {
			Logger.wtf(exception.getMessage());
		}
		return new UUID(mszDevIDShort.hashCode(), serial.hashCode()).toString();
	}
}
